<?php

namespace Gds\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Gds\UserBundle\Form\EditInfoForm;
class UserController extends Controller {
    public function getAllDepartementAction(){
        $em=  $this->getDoctrine()->getEntityManager();
        $departements=$em->getRepository('GdsArticleBundle:Departement')->findAll();
        return $this->render('GdsUserBundle:Security:listDepartement.html.twig',array('departements'=>$departements));
    }
    /**
     * @Secure(roles="ROLE_ADMIN")
     */
     public function listAction(){
         $repository= $this->getDoctrine()->getEntityManager()->getRepository('GdsUserBundle:User');
         $users=$repository->findAll();
         return $this->render('GdsUserBundle:User:list.html.twig',array('users'=>$users));
     }
     
     public function editAction(){
         $request= $this->getRequest();
         $id=$request->get('id');
         $user=  $this->getDoctrine()->getEntityManager()->getRepository('GdsUserBundle:User')->find($id);
         if(!$this->getUser()->hasRole('ROLE_SUPER_ADMIN') && ($user->hasRole('ROLE_SUPER_ADMIN') || ($user->getDepartement() != $this->getUser()->getDepartement()))){
             throw new AccessDeniedHttpException();
         }
         return $this->render('GdsUserBundle:User:show.html.twig',array('user'=>$user));
     }
     /**
     * @Secure(roles="ROLE_ADMIN")
     */
     public function editInfoGeneraleAction(){
         $request= $this->getRequest();
         $em=  $this->getDoctrine()->getEntityManager();
         $repository= $em->getRepository('GdsUserBundle:User');
         $id=$request->get('id');
         $user=  $repository->find($id);
         if(!$this->getUser()->hasRole('ROLE_SUPER_ADMIN') && ($user->hasRole('ROLE_SUPER_ADMIN') || ($user->getDepartement() != $this->getUser()->getDepartement()))){
             throw new AccessDeniedHttpException();
         }
         $form=  $this->createForm(new EditInfoForm(), $user);
         $response=array();
         if($request->isMethod('post')){
             $form->bind($request);
             if($form->isValid()){
                 $user=$form->getData();
                 if(!$this->getUser()->hasRole('ROLE_SUPER_ADMIN')){
                     $user->setDepartement($this->getUser()->getDepartement());
                 }
                 $em->persist($user);
                 $em->flush();
                 $users=$repository->findAll();
                 $response['success']=true;
                 $response['contenu']=$this->renderView('GdsUserBundle:User:listUser.html.twig',array('users'=>$users));
                 $response['info']=$this->renderView('GdsUserBundle:User:show.html.twig',array('user'=>$user));
                 return new Response(json_encode($response));
             }else{
                 $response['success']=false;
                 $response['contenu']=$this->renderView('GdsUserBundle:User:editInfoGenerale.html.twig',array('form'=>$form->createView(),'user'=>$user));
                 return new Response(json_encode($response));
             }
         }
         return $this->render('GdsUserBundle:User:editInfoGenerale.html.twig',array('form'=>$form->createView(),'user'=>$user));
     }
     /**
     * @Secure(roles="ROLE_ADMIN")
     */
     function infoAction(){
         $request= $this->getRequest();
         $em=  $this->getDoctrine()->getEntityManager();
         $repository= $em->getRepository('GdsUserBundle:User');
         $id=$request->get('id');
         $user=  $repository->find($id);
         return $this->render('GdsUserBundle:User:infoGenerale.html.twig',array('user'=>$user));
     }
     /**
     * @Secure(roles="ROLE_ADMIN")
     */
     function editPhotoAction(){
         $request= $this->getRequest();
         $em=  $this->getDoctrine()->getEntityManager();
         $repository= $em->getRepository('GdsUserBundle:User');
         $id=$request->get('id');
         $user=  $repository->find($id);
         $user->setPhoto($request->get('photo'));
         $em->persist($user);
         $em->flush();
         return new Response($user->getPhoto());
     }
    }
